package com.k2.gulimall.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.TypeReference;
import com.k2.gulimall.member.entity.MemberEntity;
import com.k2.gulimall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.k2.gulimall.member.entity.MemberLevelEntity;
import com.k2.gulimall.member.service.MemberLevelService;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.R;



/**
 * 会员等级
 *
 * @author changchaojie
 * @email changchaojie@gmail.com
 * @date 2020-11-09 17:36:10
 */
@RestController
@RequestMapping("member/memberlevel")
public class MemberLevelController {
    @Autowired
    private MemberLevelService memberLevelService;
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberLevelService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberLevelEntity memberLevel = memberLevelService.getById(id);

        return R.ok().put("memberLevel", memberLevel);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberLevelEntity memberLevel){
        if(memberLevelService.getLevelNameToMemberLevel(memberLevel.getName()).size()>0){
            return R.error().put("msg","等级名称重复");
        }else{
            updataDefault(memberLevel);
            memberLevelService.save(memberLevel);
            return R.ok();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberLevelEntity memberLevel){
        updataDefault(memberLevel);
		memberLevelService.updateById(memberLevel);
        return R.ok();
    }

    public void updataDefault(MemberLevelEntity memberLevel){
        //获取默认等级
        MemberLevelEntity defaultLevel = getMemberLevelToDefault().getData(new TypeReference<MemberLevelEntity>(){});
        //判断添加或更改后的等级是否为默认
        if(memberLevel.getDefaultStatus()==1){//是
            //判断它跟默认的等级是否重复
            if(!defaultLevel.getName().equals(memberLevel.getName())){//重复
                defaultLevel.setDefaultStatus(0);//更改之前的默认等级
                //保存
                memberLevelService.saveOrUpdate(defaultLevel);
            }
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberLevelService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 批量删除用户等级
     * @param ids 等级id数组
     * @return
     */
    @PostMapping("/batchdeletememberlevel")
    public R batchdeletememberlevel(@RequestBody Long[] ids){
        Map<String,Object> map = new HashMap<>();
        if(memberService.getUserlevelId(ids).size()==0){
            memberLevelService.removeByIds(Arrays.asList(ids));
            map.put("code",1);
            map.put("msg","success");
            return R.ok(map);
        }else{
            return R.error(0,"等级正在使用中，无法删除");
        }
    }

    /**
     * 获取默认等级
     * @return
     */
    @GetMapping("/getMemberLevelToDefault")
    public R getMemberLevelToDefault(){
        return R.ok().setData(memberLevelService.getMemberLevelToDefault());
    }





}
